package de.szut.traineeslife.adapter;

import java.io.File;
import java.util.ArrayList;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RadioButton;
import android.widget.TextView;
import de.szut.traineeslife.R;

public class SelectReportModelAdapter extends ArrayAdapter<File> {

	private Context mContext;
	private ArrayList<File> mDirectories;
	private int selectedIndex = -1;
	private boolean pathIsSelected = false;

	/**Is The Adaoter to Display the files that we found in our select Folder from SearchModelFragment
	 * @param context
	 * @param textViewResourceId
	 * @param directories
	 */
	public SelectReportModelAdapter(Context context, int textViewResourceId, ArrayList<File> directories) {
		super(context, textViewResourceId, directories);
		mContext = context;
		mDirectories = directories;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		ItemView itemView = new ItemView();
		if (v == null) {
			LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.singlechoice_list_item, parent, false);
			itemView.fileName = (TextView) v.findViewById(R.id.model_pahtname);
			itemView.modelSelected = (RadioButton) v.findViewById(R.id.model_selected);
			v.setTag(itemView);
		}else{
			itemView = (ItemView) v.getTag();
		}		
		String filePath = mDirectories.get(position).getName();
		if (filePath != null) {
			itemView.fileName.setText(filePath);
		}
		if (selectedIndex == position) {
			itemView.modelSelected.setChecked(true);
			setPathIsSelected(true);
		} else {
			itemView.modelSelected.setChecked(false);
			setPathIsSelected(false);
		}
		return v;
	}

	private static class ItemView {
		public TextView fileName;
		public RadioButton modelSelected;
	}

	@Override
	public int getCount() {
		return mDirectories.size();
	}

	public void setSelectedIndex(int index) {
		selectedIndex = index;
	}

	public boolean isPathIsSelected() {
		return pathIsSelected;
	}

	public void setPathIsSelected(boolean pathIsSelected) {
		this.pathIsSelected = pathIsSelected;
	}
}
